package com.bbyl.model.net.api;

import com.bbyl.ui.BbylApplication;
import com.bbyl.utils.Constants;
import com.binbinyl.model.local.sp.SpHelper;
import com.binbinyl.utils.AesUtils;
import com.google.gson.Gson;

import java.util.HashMap;

import okhttp3.RequestBody;

/**
 * api
 */
public class ApiHelper {
    private static ApiHelper instance = new ApiHelper();
    private static AesUtils aesEncrypter;
    private IApiService apiService = null;

    private static String sDeviceBrand;
    private static String sOsVersion;
    private static String sChannel;
    private static String deviceId;

    private ApiHelper() {
//        apiService = LyApiFactory.getInstance().getApi();
//        sDeviceBrand = SystemUtils.getDeviceBrand();
//        sOsVersion = SystemUtils.getSystemModel() + "#" + SystemUtils.getSystemVersion();
//        sChannel = MyPreferencesManager.getString("channel", "DEFAULT_CHANNEL");
        deviceId = SpHelper.INSTANCE.getDeviceId(BbylApplication.getInstance());
        try {
            aesEncrypter = new AesUtils();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static ApiHelper getInstance() {
        return instance;
    }

    /**
     * 获取临时token
     */
    public HashMap<String, Object> getInterfaceTokenPamras() {

        HashMap<String, Object> tokenMap = new HashMap<>();
        tokenMap.put("os", Constants.INTERFACE_OS);
        tokenMap.put("osv", Constants.INTERFACE_VERSION);
        String tokenKey;
        try {
            tokenKey = aesEncrypter.encrypt("2|" + deviceId);
            tokenMap.put("data", tokenKey);
            tokenMap.put("device_id", aesEncrypter.encrypt(deviceId));
            tokenMap.put("c_brand", sDeviceBrand);
            tokenMap.put("c_model", sOsVersion);
            tokenMap.put("c_channel", sChannel);
//            tokenMap.put("c_network", MyPreferencesManager.getNetState());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return tokenMap;
    }

    /**
     * 通用请求参数
     * "c_brand":"apple",
     * "c_model":"6splus 10.2",
     * "c_channel":"appstore",
     * "c_network":"wifi"
     *
     * @return
     */
    public HashMap<String, Object> createGeneralResultParamMap() {
        HashMap<String, Object> paramsMap;
        paramsMap = new HashMap<>();
        paramsMap.put("uid", SpHelper.INSTANCE.getUid());
        paramsMap.put("os", Constants.INTERFACE_OS);
        paramsMap.put("osv", Constants.INTERFACE_VERSION);
        paramsMap.put("token", SpHelper.INSTANCE.getToken());
//        paramsMap.put("c_brand", sDeviceBrand);
//        paramsMap.put("c_model", sOsVersion);
//        paramsMap.put("c_channel", sChannel);
//        paramsMap.put("c_network", MyPreferencesManager.getNetState());

        try {
            paramsMap.put("device_id", aesEncrypter.encrypt(deviceId));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return paramsMap;
    }

    /**
     * create RequestBody
     *
     * @param paramMap
     * @return
     */
    RequestBody convertMapToRequestBody(String functionName, HashMap<String, Object> paramMap) {
        String strEntity = "";
        Gson gson = new Gson();
        strEntity = gson.toJson(paramMap);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), strEntity);
        return body;
    }

    // no param json log
    public RequestBody convertMapToRequestBody(HashMap<String, Object> paramMap) {
        return convertMapToRequestBody("", paramMap);
    }
}
